﻿using SharpDX.Direct3D11;

namespace Engine.Graphics.Shaders
{
    public static class StandardBlendStates
    {
        #region Private Fields

        private static DeviceContext deviceContext;

        #endregion Private Fields

        #region Public Properties

        /// <summary>
        /// Sets Blendstate with no Alpha
        /// </summary>
        public static BlendState NoAlpha { get; set; }

        /// <summary>
        /// Sets Blendstate with Alpha
        /// </summary>
        public static BlendState WithAlpha { get; set; }

        #endregion Public Properties

        #region Public Methods

        public static void Init(Device device)
        {
            deviceContext = device.ImmediateContext;

            var blendDesc = new BlendStateDescription();
            blendDesc.AlphaToCoverageEnable = false;
            blendDesc.IndependentBlendEnable = false;
            blendDesc.RenderTarget[0].BlendOperation = BlendOperation.Add;
            blendDesc.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
            blendDesc.RenderTarget[0].DestinationBlend = BlendOption.Zero;
            blendDesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero;
            blendDesc.RenderTarget[0].SourceBlend = BlendOption.One;
            blendDesc.RenderTarget[0].SourceAlphaBlend = BlendOption.One;
            blendDesc.RenderTarget[0].IsBlendEnabled = false;
            blendDesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

            StandardBlendStates.NoAlpha = new BlendState(device, blendDesc);

            // With Alpha
            blendDesc = new BlendStateDescription();
            blendDesc.AlphaToCoverageEnable = false;
            blendDesc.IndependentBlendEnable = false;

            blendDesc.RenderTarget[0].IsBlendEnabled = true;
            blendDesc.RenderTarget[0].BlendOperation = BlendOperation.Add;
            blendDesc.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha;
            blendDesc.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha;
            blendDesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            blendDesc.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
            blendDesc.RenderTarget[0].SourceAlphaBlend = BlendOption.SourceAlpha;
            blendDesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.InverseSourceAlpha;

            StandardBlendStates.WithAlpha = new BlendState(device, blendDesc);
        }

        #endregion Public Methods
    }
}